package com.add;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SetBossTable {
  private static final Log _log = LogFactory.getLog(SetBossTable.class);
  
  private static SetBossTable _instance;
  
  private final Map<Integer, L1SetBoss> _ConfigQuestIndex = new HashMap<>();
  
  public static SetBossTable get() {
    if (_instance == null)
      _instance = new SetBossTable(); 
    return _instance;
  }
  
  public void loadSetQuest() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM w_bosschallenge");
      rs = pstm.executeQuery();
      fillSystemMessage(rs);
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("读取->w_Boss挑战副本档资料数量: " + this._ConfigQuestIndex.size() + "(" + timer.get() + "ms)");
  }
  
  private void fillSystemMessage(ResultSet rs) throws SQLException {
    while (rs.next()) {
      int Id = rs.getInt("code");
      String Message = rs.getString("set");
      L1SetBoss Quest = new L1SetBoss();
      Quest.set_id(Id);
      Quest.set_message(Message);
      this._ConfigQuestIndex.put(Integer.valueOf(Id), Quest);
    } 
  }
  
  public L1SetBoss getTemplate(int Id) {
    return this._ConfigQuestIndex.get(Integer.valueOf(Id));
  }
}
